//
//  HRZGoodsTypesView.m
//  EMarket
//
//  Created by Tyrone on 2018/5/24.
//
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZGoodsTypesView.h"

// Controllers

// Models
#import "HRZGoodsTypeModel.h"
// Views
#import "HRZGoodsTypeChooseCell.h"
// Vendors

// Categories

// Others
#import "HRZLayoutBase.h"

#define CellWidth       60
#define CellHeight      30
#define CellCornerRadius    15

@interface HRZGoodsTypesView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collection;

@end

static NSString * const HRZGoodsTypeChooseCellID = @"HRZGoodsTypeChooseCell";

@implementation HRZGoodsTypesView

- (UICollectionView *)collection{
    if (!_collection) {
        HRZLayoutBase *layout = [HRZLayoutBase new];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//        layout.minimumLineSpacing = 10;
//        layout.minimumInteritemSpacing = 10;
        layout.sectionHeadersPinToVisibleBounds = YES;
        
        _collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collection.delegate = self;
        _collection.dataSource = self;
        [self addSubview:_collection];
        
        _collection.frame = CGRectMake(0, 10, kScreenWidth, 30);
        _collection.showsVerticalScrollIndicator = NO;
        _collection.showsHorizontalScrollIndicator = NO;
        
        
        /** 注册 */
        [_collection registerNib:[UINib nibWithNibName:NSStringFromClass([HRZGoodsTypeChooseCell class]) bundle:nil] forCellWithReuseIdentifier:HRZGoodsTypeChooseCellID];
    }
    return _collection;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [self.collection setBackgroundColor:HRZBGColor];
    [_collection selectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
}


#pragma mark - ===============  setting  ===============
- (void)setGoodsTypeArray:(NSMutableArray<HRZGoodsTypeModel *> *)goodsTypeArray {
    _goodsTypeArray = goodsTypeArray;
    [_collection reloadData];
}

#pragma mark - ===============  UICollectionViewDataSource  ===============
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _goodsTypeArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HRZGoodsTypeChooseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HRZGoodsTypeChooseCellID forIndexPath:indexPath];
    cell.goodsType = _goodsTypeArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate respondsToSelector:@selector(onGoodsTypesViewDidSelect:)]) {
        [self.delegate onGoodsTypesViewDidSelect:indexPath];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(CellWidth, CellHeight);
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = CGRectMake(0, 0, CellWidth, CellHeight);
    
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.frame = CGRectMake(0, 0, CellWidth, CellHeight);
    borderLayer.lineWidth = 1.f;
    borderLayer.strokeColor = HRZThemeColor.CGColor;
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, CellWidth, CellHeight) cornerRadius:CellCornerRadius];
    maskLayer.path = bezierPath.CGPath;
    borderLayer.path = bezierPath.CGPath;
    
    [cell.contentView.layer insertSublayer:borderLayer atIndex:0];
    [cell.layer setMask:maskLayer];
}


@end
